﻿using System;

namespace ZHello.Utils
{
    public static class Enums
    {
        public static string ToStringEx(this Enum value)
        {
            var ret = value.ToString();
            Type enumType = value.GetType();
            string name = Enum.GetName(enumType, value);
            if (name == null)
            {
                return ret;
            }
            System.Reflection.FieldInfo fieldInfo = enumType.GetField(name);
            if (fieldInfo == null)
            {
                return ret;
            }
            // 获取描述的属性
            var attr = Attribute.GetCustomAttribute(fieldInfo, typeof(System.ComponentModel.DescriptionAttribute), false) as System.ComponentModel.DescriptionAttribute;
            if (attr != null)
            {
                ret = attr.Description;
            }
            return ret;
        }
    }
}